<?php
/**
 * 后台管理
 * @author jianhui
 *
 */
class Admin extends MY_Controller{
    function Admin(){
        parent::MY_Controller();
        $this->user['userRole'] == 5 || showError('您无权进入此页面');
        $tab = $this->input->get('tab');
        $this->viewData['tab'] = $tab ? $tab : 'index';
    }
    /**
     * 首页
     */
    function index(){
        $this->load->view('Admin/index', $this->viewData);
    }
    /**
     * 时段列表
     */
    function timeUnitList(){
        $this->load->model('timeUnitModel');
        //年份
        $this->viewData['yearList'] = $this->timeUnitModel->getCacheData('yearList');
        $where = array();
        $year = $this->input->get('year');
        $where['year'] = $year ? $year : $this->config->item('nowYear');
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getList($where, '', '', '', 'orderNum asc,id asc');
        $this->viewData['year'] = $where['year'];
        $this->load->view('Admin/timeUnitList', $this->viewData);
    }
    /**
     * 增加或修改时段
     */
    function timeUnitAdd(){
        $this->load->model('timeUnitModel');
        if ($_POST){
            $id = $this->input->post('id');
            $data = array(
                'year'  => (int)$this->input->post('year'),
                'name'  => $this->input->post('name'),
                'orderNum'  => (int)$this->input->post('orderNum'),
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'singleBudgetApplyStartTime'    => $this->input->post('singleBudgetApplyStartTime'),
                'singleBudgetApplyEndTime'      => $this->input->post('singleBudgetApplyEndTime'),
                'singleBudgetAuditStartTime'    => $this->input->post('singleBudgetAuditStartTime'),
                'singleBudgetAuditEndTime'      => $this->input->post('singleBudgetAuditEndTime'),
                'singleSettleApplyStartTime'    => $this->input->post('singleSettleApplyStartTime'),
                'singleSettleApplyEndTime'      => $this->input->post('singleSettleApplyEndTime'),
                'singleSettleAuditStartTime'    => $this->input->post('singleSettleAuditStartTime'),
                'singleSettleAuditEndTime'      => $this->input->post('singleSettleAuditEndTime'),
                'singleSettleScoreStartTime'    => $this->input->post('singleSettleScoreStartTime'),
                'singleSettleScoreEndTime'      => $this->input->post('singleSettleScoreEndTime'),
                'singleSettleChangeStartTime'   => $this->input->post('singleSettleChangeStartTime'),
                'singleSettleChangeEndTime'     => $this->input->post('singleSettleChangeEndTime'),
            );
            $data['year'] || showError('请输入年份');
            $data['year'] >= 2015 || showError('年份大于或等于2015年');
            $data['name'] || showError('请输入时段名称');
            $data['orderNum'] || $data['orderNum'] = 0;
            $data['startTime'] || showError('请选择时段开始时间');
            $data['endTime'] || showError('请选择时段结束时间');
            date('d', strtotime($data['startTime'])) == 1 || showError('开始时间非当月第一天');
            //结束时间戳
            $endTime = strtotime($data['endTime']);
            date('d', $endTime) == date('t', $endTime) || showError('结束时间非当月最后一天');
            if ($this->timeUnitModel->getData(array('year'=>$data['year'],'name'=>$data['name'],'id !='=>$id))){
                showError('已存在同名时段');
            }
            //默认初始化时间
            //预算申请开始时间默认为【时段开始前十天】
            $data['singleBudgetApplyStartTime'] || $data['singleBudgetApplyStartTime'] = date("Y-m-d H:i:s", strtotime($data['startTime'].' -10 day'));
            //预算申请结束时间默认为【时段结束时间】            
            $data['singleBudgetApplyEndTime'] || $data['singleBudgetApplyEndTime'] = $data['endTime'];
            //预算审核开始时间默认为【预算申报开始时间】
            $data['singleBudgetAuditStartTime'] || $data['singleBudgetAuditStartTime'] = $data['singleBudgetApplyStartTime'];
            //预算审核结束时间默认为【时段结束后15日内】
            $data['singleBudgetAuditEndTime'] || $data['singleBudgetAuditEndTime'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +15 day'));
            //结算申报开始时间默认为【时段开始时间】
            $data['singleSettleApplyStartTime'] || $data['singleSettleApplyStartTime'] = $data['startTime'];
            //结算申报结束时间默认为【时段结束后10日内】
            $data['singleSettleApplyEndTime'] || $data['singleSettleApplyEndTime'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +10 day'));
            //结算审核开始时间默认为【时段开始时间】
            $data['singleSettleAuditStartTime'] || $data['singleSettleAuditStartTime'] = $data['startTime'];
            //结算审核结束时间默认为【时段结束后20天内】  
            $data['singleSettleAuditEndTime'] || $data['singleSettleAuditEndTime'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //结算评分开始时间默认为【时段开始时间】
            $data['singleSettleScoreStartTime'] || $data['singleSettleScoreStartTime'] = $data['startTime'];
            //结算评分开始时间默认为【时段结束后20天内】
            $data['singleSettleScoreEndTime'] || $data['singleSettleScoreEndTime'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +20 day'));
            //专营店活动金额修改开始时间
            $data['singleSettleChangeStartTime'] || $data['singleSettleChangeStartTime'] = $data['startTime'];
            //专营店活动金额修改结束时间
            $data['singleSettleChangeEndTime'] || $data['singleSettleChangeEndTime'] = date("Y-m-d H:i:s", strtotime($data['endTime'].' +30 day'));
            
            if ($id){
                $timeId = $this->timeUnitModel->edit($id, $data);
            }else{
                $timeId = $this->timeUnitModel->add($data);
            }
            if ($timeId){
                $this->timeUnitModel->createTimeUnitCache();
                showSuccess('时段保存成功');
            }else{
                showError('时段保存失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            if ($id){
                $this->viewData['timeUnitData'] = $this->timeUnitModel->getData($id);
                $this->viewData['timeUnitData'] || showError('数据不存在');
            }
            $this->load->view('Admin/timeUnitAdd', $this->viewData);
        }
    }
    /**
     * 更新评分
     * @return [type] [description]
     */
    function updateScore(){
        $this->load->model('singleProcessModel');
        $list = $this->singleProcessModel->getList(array('score > '=>1));
        echo count($list);
        foreach ($list as $k => $v) {
            $this->singleProcessModel->edit(array('id'=>$v['id']),array('score'=>1));
            $this->singleProcessModel->updateMoney($v['id']);
        }
    }
}